package com.wbs.shop.nuby.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wbs.shop.nuby.commom.Key;
import com.wbs.shop.nuby.jsbridge.core.JsCallback;
import com.wbs.shop.nuby.utils.Logger;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private static JsCallback jsCallback;
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Key.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Logger.e("jsCallback==>" + (jsCallback != null));
        Logger.e("resp.errCode==>" + (resp.errCode));
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (jsCallback != null) {
                try {
                    jsCallback.call(resp.errCode + "");
                    jsCallback = null;
                    finish();
                } catch (Exception e) {
                    Logger.e(e);
                }
            }
        }
    }

    public static void setJsCallback(JsCallback jsCallback) {
        WXPayEntryActivity.jsCallback = jsCallback;
    }
}